#include<iostream>
#include<cstring>
using namespace std;

const int N=3e6+10;
int tree[N][62],p[N];
int idx;
int hash_ind(char ch)
{
	if(ch >= 'a' && ch <= 'z') return ch - 'a';
	else if(ch >= 'A' && ch <= 'Z') return ch - 'A' + 26;
	else return ch - '0' + 52;
}
void insert(string s)
{
	int cur=0;
	p[cur]++;
	for(auto&e:s)
	{
		int path=hash_ind(e);
		if(tree[cur][path]==0)
			tree[cur][path]=++idx;
		cur=tree[cur][path];
		p[cur]++;
	}
}

int find_pre(string &s)
{
	int cur=0;
	for(auto&e:s)
	{
		int path=hash_ind(e);
		if(tree[cur][path]==0)
			return 0;
		cur=tree[cur][path];
	}
	return p[cur];
}
int main()
{
	int t;cin>>t;
	while(t--)
	{
		int n,q;cin>>n>>q;

		memset(tree,0,sizeof(int)*idx*62);
		memset(p,0,sizeof(int)*idx);
		idx=0;
		while(n--)
		{
			string s;cin>>s;
			insert(s);
		}

		while(q--)
		{
			string s;cin>>s;
			cout<<find_pre(s)<<endl;
		}
	}
}